package business;
import java.io.Serializable;
import java.util.List;

import model.AcessoMenuOpcao;
import model.AcessoUsuario;
import model.AcessoUsuarioPermissao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

public class AcessoUsuarioPermissaoDAO implements Serializable{
	private static final long serialVersionUID = 1542465963934196725L;
	static AcessoUsuarioPermissaoDAO instance;
	private String opcaoDescricao 	= "";
	private String opcaoLink		= "";
	
	public static AcessoUsuarioPermissaoDAO getInstance() {
		if(instance == null)
			instance = new AcessoUsuarioPermissaoDAO();
		return instance;
	}
	
	public AcessoUsuarioPermissaoDAO() {
	}
	
	public AcessoUsuario get(String login) {
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		
		AcessoUsuario acessoUsuario  = (AcessoUsuario) session.get(AcessoUsuario.class,login);		
		tr.commit();
		
		return acessoUsuario;
	}
	
	public AcessoUsuarioPermissao getOpcao(String item) {
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		
		AcessoUsuarioPermissao opcao = (AcessoUsuarioPermissao) session.get(AcessoUsuarioPermissao.class, item);		
		tr.commit();
		
		return opcao;
	}
	
	@SuppressWarnings("unchecked")
	public List<AcessoUsuarioPermissao> getMenu(AcessoUsuario acessoUsuario) {
		
		List<AcessoUsuarioPermissao> menu;
		Session session = HibernateUtil.getSession();
		@SuppressWarnings("unused")
		Transaction tr = session.beginTransaction();
		// Usando Criteria
		Criteria cr = session.createCriteria(AcessoUsuarioPermissao.class);
		cr.add(Restrictions.eq("login", acessoUsuario.getLogin()));
		menu = cr.list();
		
		// Usando Query
//		Query qr = session.createQuery("");
//		menu = qr.list();
		return menu;
	}
	
	@SuppressWarnings("unchecked")
	public boolean getAcessoMenuOpcao(AcessoUsuario acessoUsuario, AcessoMenuOpcao opcao) {
		this.opcaoDescricao = "";
		this.opcaoLink		= "";
		List<AcessoUsuarioPermissao> menu;
		Session session = HibernateUtil.getSession();
		@SuppressWarnings("unused")
		Transaction tr = session.beginTransaction();
		// Usando Criteria
		Criteria cr = session.createCriteria(AcessoUsuarioPermissao.class);
		cr.add(Restrictions.eq("login", acessoUsuario.getLogin()));
		cr.add(Restrictions.eq("item", opcao.getOpcao()));
		menu = cr.list();
		if(menu == null) {
			return false;
		}
		if(menu.size() > 0) {
			this.opcaoDescricao = menu.get(0).getDescricao();
			this.opcaoLink		= menu.get(0).getLink();
		}
		return (menu.size() > 0);
	}

	public String getOpcaoDescricao() {
		return opcaoDescricao;
	}

	public void setOpcaoDescricao(String opcaoDescricao) {
		this.opcaoDescricao = opcaoDescricao;
	}

	public String getOpcaoLink() {
		return opcaoLink;
	}

	public void setOpcaoLink(String opcaoLink) {
		this.opcaoLink = opcaoLink;
	}
}